/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.verolux.soft.debaraz.service;

import com.verolux.soft.debaraz.ApplicationException;
import com.verolux.soft.debaraz.bean.Article;
import com.verolux.soft.debaraz.dao.ArticleDAO;
import com.verolux.soft.debaraz.utils.ArticleImageUploader;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author ut1e8p
 */
@Service(value="articleService")
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleDAO articleDAO;

    @Autowired
    private ArticleImageUploader articleImageUploader;
    
    /**
     *
     * @param article
     * @throws ApplicationException
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = ApplicationException.class)
    public void create(Article article) throws ApplicationException {
        try {
            if (article.getId() != null) {
                article.setId(null);
            }
            articleDAO.save(article);

            articleImageUploader.setArticle(article);
            articleImageUploader.upload();

            articleDAO.save(article);
        } catch (Exception e) {
            throw new ApplicationException(e.getMessage(), e);
        }
    }

    /**
     * 
     * @param selloutId
     * @return
     * @throws ApplicationException
     */
    @Transactional(propagation = Propagation.REQUIRED, readOnly=true)
    public List<Article> getSelloutArticles(Long selloutId) throws ApplicationException {
        try {
            List<Article> articles = articleDAO.findAllBySelloutId(selloutId);
            return articles;
        } catch (Exception e) {
            throw new ApplicationException(e.getMessage(), e);
        }
    }
}
